package com.shop.front.controller.store;

import com.shop.front.vo.store.StoreProductReplyCountVo;
import com.shop.domain.store.StoreProductReply;
import com.shop.front.request.store.StoreProductReplyAddReq;
import com.shop.front.service.store.StoreProductReplyService;
import com.shop.front.shiro.UserTokenUtil;
import com.shop.util.IdSignUtil;
import com.shop.web.context.WebContext;
import com.shop.web.response.BaseResponse;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import java.util.Map;

@Api(description = "产品评论相关", position = -1)
@RestController
@RequestMapping(value = "/storeProductReply", method = RequestMethod.POST, produces = {"application/json;charset=UTF-8"})
@Slf4j
public class StoreProductReplyController {

    @Autowired
    private StoreProductReplyService storeProductReplyService;

    @ApiOperation(value = "商品评论统计")
    @ApiResponses(@ApiResponse(code = 10000, response = StoreProductReplyCountVo.class, message = "content"))
    @ApiImplicitParams(@ApiImplicitParam(name="sign", required = true, value = "商品id", paramType = "body"))
    @RequestMapping("/count")
    public BaseResponse count(@RequestBody @ApiIgnore Map<String, String> map) {
        Long id = WebContext.getSignId();
        if(null == id) {
            return BaseResponse.CHECK_SIGN_IS_FAIL;
        }

        // 商品评论数量、好评数、中评数、差评数、好评率
        StoreProductReplyCountVo productReplyCountVo = storeProductReplyService.count(id);

        return new BaseResponse(productReplyCountVo);
    }

    @ApiOperation(value = "商品评论列表")
    @ApiResponses(@ApiResponse(code = 10000, response = StoreProductReply.class, message = "content"))
    @ApiImplicitParams({@ApiImplicitParam(name="sign", required = true, value = "商品id", paramType = "body"),
                        @ApiImplicitParam(name="type", required = false, value = "评价类型 1 好评 2 中评 3 差评", paramType = "body")})
    @RequestMapping("/page")
    public BaseResponse page(@RequestBody @ApiIgnore Map<String, String> map) {
        Integer type = null;
        if(StringUtils.isNotBlank(map.get("type"))) {
            type = Integer.valueOf(map.get("type"));
        }

        Long productId = WebContext.getSignId();
        if(null == productId) {
            return BaseResponse.CHECK_SIGN_IS_FAIL;
        }

        return storeProductReplyService.page(productId, type);
    }

    @ApiOperation(value = "提交评论")
    @ApiResponses(@ApiResponse(code = 10000, response = String.class, message = "content"))
    @RequestMapping("/addReply")
    public BaseResponse addReply(@RequestBody @Validated StoreProductReplyAddReq storeProductReplyAddReq) {
        Long orderCartInfoId = IdSignUtil.checkSign(storeProductReplyAddReq.getOrderCartInfoSign());
        if(null == orderCartInfoId) {
            return BaseResponse.CHECK_SIGN_IS_FAIL;
        }
        storeProductReplyAddReq.setOrderCartInfoId(orderCartInfoId);

        Long userId = UserTokenUtil.getUser().getId();

        return storeProductReplyService.addReply(userId, storeProductReplyAddReq);
    }

}
